<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   Atwix
 * @package    Atwix_WesternUnionPayment
 */


class Atwix_WesternUnionPayment_Model_Quote_Address_Total_Westernunionpayment extends
 Mage_Sales_Model_Quote_Address_Total_Abstract
{
    public function __construct()
    {
        $this->setCode('payment');   
    }
 
    /**
     * Collect totals information about Western Union
     *
     * @param   Mage_Sales_Model_Quote_Address $address
     * @return  Mage_Sales_Model_Quote_Address_Total_Westernunionpayment
     */
    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        parent::collect($address);
        try{
            $quote = $address->getQuote();
            if(!$quote)return $this;
            if($quote->getPaymentsCollection()->count()==0)return $this;
            $payment = $quote->getPayment();
            if($payment && $payment->getMethod() == 'westernunionpayment') {
            	$wp = Mage::getModel('westernunionpayment/paymentMethod');
            	$handlingFee = floatval($wp->getConfigData('handling_fee'));
            	if (empty($handlingFee))
            	{
            		return $this;
            	}   
            	$handlingType = $wp->getConfigData('handling_type');       	
                $TotalAmount = 0;
                $thisAmount = 0;
                foreach($address->getAllBaseTotalAmounts() as $amount)
                {
                   $TotalAmount+= $amount;
                }
                 
                if($handlingType == Atwix_WesternUnionPayment_Model_PaymentMethod::HANDLING_TYPE_PERCENT) {
                    $thisAmount =   $TotalAmount * ($handlingFee/100); 
                } else {
                    $thisAmount = $handlingFee;
                }
                 
                $thisAmount =  $quote->getStore()
                    ->roundPrice($thisAmount);
                 
                 
                $basePayment = $thisAmount;
                $payment = $quote->getStore()->convertPrice($thisAmount, false);
                $this->_setAmount($payment)->_setBaseAmount($basePayment); 
                $address->setPaymentAmount($payment);
                $address->setBasePaymentAmount($basePayment);
                $address->setPaymentDescription(trim($this->getLabel()));
            }
             
        } catch(Exception $e) {
            Mage::throwException($e->getMessage());
            //do nothing.   
        }
        return $this;
    }
 
    /**
     * Add Western Union totals information to address object
     *
     * @param   Mage_Sales_Model_Quote_Address $address
     * @return  Mage_Sales_Model_Quote_Address_Total_Westernunionpayment
     */
    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
        parent::fetch($address);
        $amount = $address->getTotalAmount($this->getCode());
        if ($amount!=0) {
            $address->addTotal(array(
                'code'=>$this->getCode(),
                'title'=>$this->getLabel(),
                'value'=>$amount
            ));
        }
         
        return $this;
    }
 
    /**
     * Get label
     *
     * @return string
     */
    public function getLabel()
    {
        return Mage::helper('westernunionpayment')->__('Western Union Discount');
    }
}
